<template>
  <div class="app-container">
    <div class="queryform">
      <el-form
        :model="form"
        ref="form"
        label-width="97px"
        label-position="right"
      >
        <el-form-item label="场景名称:" prop="sceneId">
          <el-select v-model="form.sceneId" clearable>
            <el-option
              v-for="item in sceneList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规则名称:" prop="ruleName">
          <el-input
            v-model.trim="form.ruleName"
            maxlength="128"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="风险属性:" prop="riskField">
          <el-select v-model="form.riskField" clearable>
            <el-option
              v-for="item in riskFieldList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规则类型:" prop="ruleType">
          <el-select v-model="form.ruleType" clearable>
            <el-option
              v-for="item in ruleTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="refresh">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 操作栏 -->
    <div class="buttons">
      <el-button type="primary" icon="el-icon-plus" @click="add"
        >新增</el-button
      >
    </div>
    <div class="mod-result-table">
      <HTable
        ref="table"
        :tableData="tableData"
        :columnConfigs="columnConfigs"
        @selection-change="handleSelectionChange"
        :selectable="false"
        orderable
        v-loading="loading"
        :pages="pagination"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
      >
        <template slot-scope="{ row }">
          <template v-if="row.name == 'action'">
            <el-button
              type="text"
              size="small"
              icon="el-icon-edit"
              @click="edit(row.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              size="small"
              @click="handleDelete(row.row)"
              >删除</el-button
            >
          </template>
          <template v-else-if="row.name == 'sceneId'">
            {{ $getOptionLabel(row.row.sceneId, sceneList) }}
          </template>
          <template v-else-if="row.name == 'riskField'">
            {{ $getOptionLabel(row.row.riskField, riskFieldList) }}
          </template>
          <template v-else-if="row.name == 'ruleType'">
            {{ $getOptionLabel(row.row.ruleType, ruleTypeList) }}
          </template>
          <template v-else-if="row.name == 'transparency'">
            {{ row.row.transparency && formatTrancy(row.row.transparency) }}
          </template>
        </template>
      </HTable>
    </div>
    <div class="vpnform">
      <rule
        ref="AddDialog"
        :dialogVisible1.sync="dialogVisible1"
        :editFlag.sync="editFlag"
        @submit="submitaddForm"
      ></rule>
    </div>
  </div>
</template>

<script>
import table from "@/mixins/commontable";
import rule from "./rule.vue";
import { columnConfigs } from "./columnConfigs.js";
import { getList, delCurRow } from "@/api/yybl/riskRule";
import { mapState } from "vuex";
export default {
  components: {
    rule,
  },
  mixins: [table], //表格的混入，主要包括表格上通用的方法和数据，比如查询，重置，分页这些功能
  props: {},
  data() {
    return {
      columnConfigs,
      dialogVisible1: false,
      editFlag: false,
      curRow: {},
      form: {
        sceneId: "",
        ruleName: "",
        riskField: "",
        ruleType: "",
      },
    };
  },
  watch: {},
  computed: {
    ...mapState("common", ["sceneList", "riskFieldList", "ruleTypeList"]), //函数中第一个是模块名称，第二个是模块中的数组
  },
  methods: {
    formatTrancy(val) {
      let a = Number(val);
      return Math.round(a * 100) + "%";
    },
    add() {
      this.editFlag = false;
      this.dialogVisible1 = true;
      this.$nextTick(() => {
        this.$refs.AddDialog.add();
      });
    },
    edit(row) {
      this.editFlag = true;
      this.dialogVisible1 = true;
      this.curRow = row;
      this.$refs.AddDialog.edit(row);
    },
    async handleDelete(row) {
      this.$confirm("此操作将删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let ids = [];
          console.log(row);
          if (row) {
            ids.push(row.id);
          }
          console.log(ids);
          const res = await delCurRow({
            ids,
          });
          if (res.rescode == "200") {
            this.$message.success("删除成功");
            this.refresh();
          } else this.$message.error(res.msg);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitaddForm(val) {
      if (val == 200) {
        this.refresh();
      }
    },
    handleClose() {
      this.dialogVisible1 = false;
    },
    handleSelectionChange() {},
    getparams() {
      const { sceneId, ruleName, riskField, ruleType } = this.form;
      return {
        sceneId,
        ruleName,
        riskField,
        ruleType,
      };
    },
    refresh() {
      this.getlists(getList, this.getparams());
    },
    resetForm() {
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
        this.refresh();
      });
    },
  },
  created() {
    this.refresh();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
.queryform {
  ::v-deep .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      max-width: 33.3%;
      flex-grow: 1;
      flex-shrink: 1;
      flex-basis: 33.3%;
    }

    // .el-form-item:nth-child(7) {
    //   max-width: 100%;
    //   flex-basis: 100%;
    //   display: flex;
    //   justify-content: end;
    // }
  }
}
::v-deep .el-form-item__content {
  .el-date-editor.el-input {
    width: 100%;
  }
}
::v-deep .el-button {
  .el-icon-edit + span {
    margin-left: 0px;
  }
  .el-icon-delete + span {
    margin-left: 0px;
  }
}
::v-deep .el-dialog {
  min-width: 640px;
  .el-dialog__header {
    border-bottom: 1px solid #e8e8e8;
  }
  .el-dialog__footer {
    border-top: 1px solid #e8e8e8;
  }
}
.addDialog {
  .el-dialog {
    min-width: 640px;
  }

  .el-form {
    .el-form-item.slider {
      width: 100%;
    }
  }
}
</style>
